#include "Rp2PixelProgram.h"
#include "Rp2Catalog.h"

using namespace Rp2;

RP2_IMPLEMENT_RTTI(Rp2, PixelProgram, Program);
RP2_IMPLEMENT_DEFAULT_NAME_ID(PixelProgram, Program);

//---------------------------------------------------------------------------------------------------
PixelProgram::PixelProgram(const std::string& rkProgramName)
{
	// Try to load compiled program 
	std::string  kFilename = std::string("p_") + rkProgramName + 
		std::string(".") + RendererType;// + std::string(".wmsp");

	bool bLoaded = Program::Load(kFilename, this);

	if (!bLoaded)
	{
		// Try to load Cg file directly.
		kFilename = rkProgramName + std::string(".cg");
		if (!Program::CompileCg(kFilename))
		{
			assert(false);
		}
		kFilename = std::string("p_") + rkProgramName + 
			std::string(".") + RendererType;// + std::string(".wmsp");
		bLoaded = Program::Load(kFilename, this);
	}
	assert(bLoaded);
	(void)bLoaded;
	SetName(rkProgramName.c_str());

    // Verify that the pixel program does not require more resources than the
    // renderer can support.
    assert(m_kInputAttributes.GetMaxColors() <= Renderer::GetMaxColors());
    assert(m_kInputAttributes.GetMaxTCoords() <= Renderer::GetMaxTCoords());
    assert((int)m_kSamplerInformation.size() <= Renderer::GetMaxPShaderImages());

	Catalog<PixelProgram>::GetActive()->Insert(this);
}
//---------------------------------------------------------------------------------------------------
PixelProgram::PixelProgram()
{

}
//---------------------------------------------------------------------------------------------------
PixelProgram::~PixelProgram()
{
	Catalog<PixelProgram>::RemoveAll(this);
}
//---------------------------------------------------------------------------------------------------
PixelProgram* PixelProgram::Load(const std::string& rkProgramName)
{
	return new PixelProgram(rkProgramName);
}
//---------------------------------------------------------------------------------------------------
